package com.ctgu.server.controller;


import com.ctgu.server.dao.dto.ArticleDto;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.service.ArticleService;
import com.ctgu.server.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ctgu
 * @since 2023-05-23
 */
@RestController
@RequestMapping("/server/article")
@CrossOrigin
@Api(tags = "文章操作接口")
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    @PostMapping("/create")
    @ApiOperation(notes = "创建文章",value = "create")
    public Result createArticle(@RequestBody ArticleDto articleDto){
        if(articleDto.getUserId()==null){
            return ResultUtil.error("用户ID不能为空！");
        }
        articleDto.setId(null);
        return  articleService.insertArticle(articleDto);
    }

    @GetMapping("/getArticle/{id}")
    @ApiOperation(notes = "通过Id获取指定文章的信息",value = "getArticle")
    public Result getArticle(@PathVariable Long id){

        return articleService.getArticleById(id);
    }
    @GetMapping("/getList")
    @ApiOperation(notes = "获取所有文章的信息",value = "getList")
    public Result getArticleList(){

        return articleService.getArticleList();
    }

    @PutMapping("/update")
    @ApiOperation(notes = "修改文章",value = "update")
    public Result updateArticle(@Validated @RequestBody ArticleDto articleDto){
        if(articleDto.getUserId()==null&&articleDto.getId()==null){
            return ResultUtil.error("用户ID不能为空！");
        }


        return articleService.updateArticleById(articleDto);
    }
}

